package com.quxiang.pay.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quxiang.pay.constant.Constant;
import com.quxiang.pay.dao.CymjDao;
import com.quxiang.pay.dao.MaqueDao;
import com.quxiang.pay.dao.XunLeDao;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private CymjDao cymjDao;

	@Autowired
	private XunLeDao xunleDao;
	@Autowired
	private MaqueDao maqueDao;

	@Override
	public int existUserByUnionId(String unionId, int type) {
		if (type == Constant.TYPE_CYMJ) {
			return cymjDao.selectOne("user.existUser", unionId);
		} else if (type == 2) {
			return xunleDao.selectOne("user.existUser", unionId);
		} else if (type == 3) {
			return maqueDao.selectOne("user.existUser", unionId);
		} else {
			return 0;
		}

	}

	public int hasAgent(String unionId, int type) {

		int result = 0;
		result = existUserByUnionId( unionId,  type);
		if(result ==0){
			//用户不存在
			result = 1;
			return result;
		}
		int userId = 0;
		if (type == Constant.TYPE_CYMJ) {
			userId = cymjDao.selectOne("user.getUser", unionId);
		} else if (type == 2) {
			userId = xunleDao.selectOne("user.getUser", unionId);
		} else if (type == 3) {
			userId = maqueDao.selectOne("user.getUser", unionId);
		}
		
		if (type == Constant.TYPE_CYMJ) {
			result = cymjDao.selectOne("user.hasAgent", userId);
		} else if (type == 2) {
			result = xunleDao.selectOne("user.hasAgent", userId);
		} else if (type == 3) {
			result = maqueDao.selectOne("user.hasAgent", userId);
		}

		// 代理不存在
		if (result == 0) {
			result = 2;
		} else {

			if (result > 1) {
				System.out.println("绑定关系冲突");
			}
			result = 3;
		}

		return result;

	}

}
